package com.yiren.manager.ctrl.test;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrElderTestLevelRecord;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.department.YrStaffInfoForm;
import com.yiren.manager.form.elder.YrElderForm;
import com.yiren.manager.form.test.YrElderTestLevelRecordForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.elder.YrElderService;
import com.yiren.manager.service.iface.test.YrElderTestLevelRecordService;
import com.yiren.manager.utils.ObjectUtils;

@RequestMapping("elderTestLevelRecord")
@Controller
public class YrElderTestLevelRecordController extends GenericController<YrElderTestLevelRecordForm, YrElderTestLevelRecord>{
	
	@Autowired
	private YrElderTestLevelRecordService selfService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@Autowired
	private YrElderService elderService;
	
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrElderTestLevelRecordForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrElderTestLevelRecordForm> result = new HTTPResult<YrElderTestLevelRecordForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrElderTestLevelRecordForm form = tool.mapper("elderTestLevelRecord", YrElderTestLevelRecordForm.class);
		if(null==form){
			throwExp("param.error");
		}
		String sqlWhere = "elder_id=?1 AND test_times_number=?2";
		
		QueryResult<YrElderTestLevelRecord> qr = selfService.find(YrElderTestLevelRecord.class, sqlWhere, new Object[]{form.getElderId(),form.getTestTimesNumber()});
		if(qr.getTotalRecord()>0){
			throwExp("该测试结果已记录，请勿重复提交");
		}
		if(null==form.getElderTestLevelRecordId()||form.getElderTestLevelRecordId().isEmpty()){
			form.setElderTestLevelRecordId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrElderTestLevelRecord entity = ObjectUtils.objectToObject(form, YrElderTestLevelRecord.class);
		selfService.save(entity);
		
		return result;
	}
	
	@RequestMapping("findOne")
	@ResponseBody
	public HTTPResult<YrElderTestLevelRecordForm> findOne(String elderId,Integer testTimesNumber) throws Exception{
		HTTPResult<YrElderTestLevelRecordForm> result = new HTTPResult<YrElderTestLevelRecordForm>();
		
		String sqlWhere = "elder_id=?1 AND test_times_number=?2";
		QueryResult<YrElderTestLevelRecord> qr = selfService.find(YrElderTestLevelRecord.class, sqlWhere, new Object[]{elderId,testTimesNumber});
		if(qr.getTotalRecord()==0){
			throwExp("查询不到相关记录");
		}
		YrElderTestLevelRecordForm form = toForm(qr.getResultList().get(0));
		YrStaffInfo staffInfo = staffInfoService.findOne(form.getStaffId());
		if(staffInfo!=null){
			form.setStaffInfoForm(ObjectUtils.objectToObject(staffInfo, YrStaffInfoForm.class));
		}
		YrElder elder = elderService.findOne(elderId);
		if(null!=elder){
			form.setElderForm(ObjectUtils.objectToObject(elder, YrElderForm.class));
		}
		
		List<YrElderTestLevelRecordForm> formList = new ArrayList<YrElderTestLevelRecordForm>();
		formList.add(form);
		
		result.setData(formList);
		return result;
	}
	
	@RequestMapping("findList")
	@ResponseBody
	public HTTPResult<YrElderTestLevelRecordForm> findList(String elderId) throws Exception{
		HTTPResult<YrElderTestLevelRecordForm> result = new HTTPResult<YrElderTestLevelRecordForm>();
		String sqlWhere = "elder_id=?1";
		
		QueryResult<YrElderTestLevelRecord> qr = selfService.find(YrElderTestLevelRecord.class, sqlWhere, new Object[]{elderId});
		List<YrElderTestLevelRecordForm> formList = new ArrayList<YrElderTestLevelRecordForm>();
		for(YrElderTestLevelRecord entity : qr.getResultList()){
			YrElderTestLevelRecordForm form = toForm(entity);
			YrElder elder = elderService.findOne(elderId);
			if(null!=elder){
				form.setElderForm(ObjectUtils.objectToObject(elder, YrElderForm.class));
			}
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	@Override
	public YrElderTestLevelRecordForm toForm(YrElderTestLevelRecord e) throws Exception {
		YrElderTestLevelRecordForm form = ObjectUtils.objectToObject(e, YrElderTestLevelRecordForm.class);
		return form;
	}

	@Override
	public YrElderTestLevelRecord toEntity(YrElderTestLevelRecordForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
